% function to write spectral matrices
% in edi format
% A lot need to be added manually in edi file
% This code may be updated to write full edi file
% latest date 24.10.2002
% 

function[fid,er] = edi_out(SPM,fid),

er=1;

fprintf(fid,'>=SPECTRASECT\n');
fprintf(fid,'  SECTID=%s\n',SPM.SiteID);
fprintf(fid,'  NCHAN=%d\n',size(SPM.spectra(1).data,1));
fprintf(fid,'  NFREQ=%d\n',length(SPM.spectra));
fprintf(fid,'  MAXBLKS=76\n');
fprintf(fid,'  //5\n');
fprintf(fid,'  0011.001  0012.001  0013.001 0014.001 0015.001\n');
fprintf(fid,'\n');
nfrq=length(SPM.spectra);
nchannel = length(SPM.spectra(1).data);
for i = 1:nfrq,
   S = sprintf('%8.3E',SPM.spectra(i).freq);
   S(8:9)=[];
   fprintf(fid,'>SPECTRA    FREQ= %s   ROTSPEC=0    BW= 9.766E-4   AVGT=%d  //%d \n',S,floor(SPM.spectra(i).dof.avgt),nchannel*nchannel);
   for j = 1:nchannel,
       for ii = 1:nchannel,
         S= sprintf('%15.7E ',SPM.spectra(i).data(j,ii));
         S(13)=[];
         fprintf(fid,'%s',S);
      end;
      
         fprintf(fid,'\n');
   end;
   fprintf(fid,' \n');
end;

fprintf(fid,'>END');
